import Vue from 'vue';
import Loading from './index.vue';

const loadingConstructor = Vue.extend(Loading);

class LoadingFactory {
    constructor() {
        this.loading = null;
    }
    static loadAmount = 0;
    create(options) {
        let amount = LoadingFactory.loadAmount;
        if (!amount) {
            this.loading = new loadingConstructor()
            this.render(options)
        } else {
            this.insertion(options)
        }
        LoadingFactory.loadAmount++
        return this
    }
    render(options) {
        if (options) {
            Object.assign(this.loading, options)
        }
        document.body.append(this.loading.$mount().$el)
        this.loading.$nextTick(() => {
            this.loading.visibled = true
        })
    }
    insertion(options) {
        if (options) {
            Object.assign(this.loading, options)
        }

    }
    remove() {
        LoadingFactory.loadAmount = 0;
        this.loading.remove()
    }
    install(vue) {
        vue.prototype.$loading = this
    }

}

export default new LoadingFactory();

